package cn.lena.idle.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.lena.idle.R;
import cn.lena.idle.pojo.Goods;

/**
 * @author lena
 * @date 2021/6/13
 */
public class MyPublishAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater layoutInflater;

    HashMap<Integer, View> location = new HashMap<>();
    private List<Goods> lists=new ArrayList<>();

    public MyPublishAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }

    /**
     * 设置列表值
     * @param lists
     */
    public void setData(List<Goods> lists){
        this.lists=lists;
        notifyDataSetChanged();
    }

    static class ViewHolder{

        public ImageView picture;
        public TextView title,address,publishTime,price,status;

        // TODO:修改
        public ViewHolder(View itemView,Goods goods){

            // 获取控件
            picture=itemView.findViewById(R.id.publish_picture);
            title=itemView.findViewById(R.id.publish_title);
            status=itemView.findViewById(R.id.publish_status);
            address=itemView.findViewById(R.id.publish_address);
            publishTime=itemView.findViewById(R.id.publish_time);
            price=itemView.findViewById(R.id.publish_price);

            // 给控件赋值
            byte[] goodsPicture=goods.getPicture();
            picture.setImageBitmap(BitmapFactory.decodeByteArray(goodsPicture,0,goodsPicture.length));
            title.setText(goods.getGoodsName());
            address.setText(goods.getAddress());
            publishTime.setText(goods.getPublishTime());
            price.setText(goods.getPrice());
            switch (goods.getStatus()){
                case 0:status.setText("未销售");break;
                case 1:status.setText("已销售");break;
                case 2:status.setText("已下架");break;
            }

        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO:修改holder类型
        MyPublishAdapter.ViewHolder holder=null;
        if(location.get(position) == null) {
            convertView = layoutInflater.inflate(R.layout.item_mypublish,null);
            Goods goods = (Goods)getItem(position);
            holder = new ViewHolder(convertView,goods);
            location.put(position,convertView);
            convertView.setTag(holder);
        }else{
            convertView = location.get(position);
            holder = (ViewHolder)convertView.getTag();
        }
        return convertView;
    }

    /**
     * 数据长度
     * @return
     */
    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


}

